[Mochi] is a functional language on top of Python 3. I was wondering how hard it woudl be to write an IPython wrapper kernel.
The basic wasn't tht hard, it requires a few patches to mochi, but works fine. You can find the wrapper kernel here
You can find the wrapper kernel here
Mochi is a dynamically typed programming language for functional programming and actor-style programming.
Its interpreter is written in Python3. The interpreter translates a program written in Mochi to Python3's AST / bytecode.
In [1]:
def factorial(n, m):
if n == 1:
m
else:
factorial(n - 1, n * m)
In [2]:
factorial(100, 1)
In [3]:
def factorial:
n: factorial(n, 1)
0, acc: acc
n, acc: factorial(n - 1, acc * n)
In [4]:
factorial(100)
In [5]:
def fizzbuzz(n):
match [n % 3, n % 5]:
[0, 0]: "fizzbuzz"
[0, _]: "fizz"
[_, 0]: "buzz"
_: n
In [6]:
range(1, 31) |> map(fizzbuzz) |> pvector() |> print
In [7]:
def show():
receive:
message:
print(message)
show()
actor = spawn(show)
send('foo', actor)
actor ! 'bar' # send('bar', actor)
sleep(1)
In [8]:
1
In [9]:
'foo' !> spawn(show)
sleep(1)
In [10]:
['foo', 'bar'] !&> spawn(show)
The meaning of the above is the same as the meaning of the following.
In [11]:
spawn(show) ! 'foo'
spawn(show) ! 'bar'
sleep(1)
In [14]:
from flask import Flask
app = Flask('demo')
@app.route('/')
def hello():
'Hello World!'
app.run()
In [15]:
macro aif(test, true_expr, false_expr):
quasi_quote:
it = unquote(test)
if it:
unquote(true_expr)
else:
unquote(false_expr)
In [16]:
aif([], first(it), "empty")
In [17]:
aif([10, 20], first(it), "empty")
$ pip3 install mochi
$ mochi
>>>
$ cat kinako.mochi
print('kinako')
$ mochi kinako.mochi
kinako
$
$ mochi -c kinako.mochi > kinako.mochic
$ mochi -e kinako.mochic
kinako
$
In [18]:
[1, 2, 3]
In [19]:
v(1, 2, 3)
In [20]:
vec = [1, 2, 3]
vec2 = vec.set(0, 8)
In [21]:
vec
In [22]:
[x, y, z] = vec
x # => 1
y # => 2
z # => 3
get(vec, 0) # => 1
get(vec, 0, 2) # => [1, 2]
{'x': 100, 'y': 200}
In [23]:
ma = {'x': 100, 'y': 200}
ma.get('x') # => 100
ma.x # => 100
ma2 = ma.set('x', 10000)
In [25]:
ma # => pmap({'y': 200, 'x': 100})
get(ma, 'y') # => 200
m(x=100, y=200)
In [26]:
s(1, 2, 3)
In [27]:
b(1, 2, 3)
In [29]:
def hoge(x):
hoge + str(x)
In [30]:
hoge(3)
In [31]:
lis = [1, 2, 3]
match lis:
[1, 2, x]: x
_: None
In [32]:
match lis:
[1, &rest]: rest
_: None
In [33]:
foo_map = {'foo' : 'bar'}
match foo_map:
{'foo' : value}: value
_: None
In [34]:
match 10:
int(x): 'int'
float(x): 'float'
str(x): 'str'
bool(x): 'bool'
_: 'other'
In [35]:
match [1, 2, 3]:
[1, str(x), 3]: 'str'
[1, int(x), 3]: 'int'
_: 'other'
In [36]:
record Mochi
record AnkoMochi(anko) < Mochi
record KinakoMochi(kinako) < Mochi
anko_mochi = AnkoMochi(anko=3)
isinstance(anko_mochi, Mochi)
In [37]:
isinstance(anko_mochi, AnkoMochi)
In [38]:
isinstance(anko_mochi, KinakoMochi)
In [39]:
match anko_mochi:
KinakoMochi(kinako): 'kinako ' * kinako + ' mochi'
AnkoMochi(anko): 'anko ' * anko + 'mochi'
Mochi(_): 'mochi'
In [40]:
record Person(name, age):
def show(self):
print(self.name + ': ' + self.age)
foo = Person('foo', '32')
foo.show()
In [42]:
x = 3000
In [43]:
[a, b] = [1, 2]
a
In [44]:
b
In [45]:
[c, &d] = [1, 2, 3]
c
In [46]:
d
In [41]:
data Point:
Point2D(x, y)
Point3D(x, y, z)
# The meaning of the above is the same as the meaning of the following.
# record Point
# record Point2D(x, y) < Point
# record Point3D(x, y, z) < Point
p1 = Point2D(x=1, y=2)
In [ ]:
p2 = Point2D(3, 4)
In [47]:
p1.x
In [48]:
data Point:
Point2D(x, y)
Point3D(x, y, z)
def offset:
Point2D(x1, y1), Point2D(x2, y2):
Point2D(x1 + x2, y1 + y2)
Point3D(x1, y1, z1), Point3D(x2, y2, z2):
Point3D(x1 + x2, y1 + y2, z1 + z2)
_: None
offset(Point2D(1, 2), Point2D(3, 4))
In [49]:
offset(Point3D(1, 2, 3), Point3D(4, 5, 6))
In [50]:
def show:
int(x), message: print('int', x, message)
float(x), message: print('float', x, message)
_: None
show(1.0, 'msg')
In [53]:
add = (x, y) -> x + y
add(1, 2)
In [54]:
add = -> $1 + $2
add(1, 2)
In [55]:
foo = (x, y) ->
if x == 0:
y
else:
x
foo(1, 2)
In [56]:
foo(0, 2)
In [57]:
pvector(map(-> $1 * 2, [1, 2, 3]))
In [58]:
add = -> $1 + $2
2 |> add(10) |> add(12)
In [59]:
None |>? add(10) |>? add(12)
In [60]:
def fizzbuzz(n):
match [n % 3, n % 5]:
[0, 0]: "fizzbuzz"
[0, _]: "fizz"
[_, 0]: "buzz"
_: n
result = range(1, 31) |> map(fizzbuzz)
pvector(result)
In [61]:
pvector(result)
In [62]:
pvector(result)
In [63]:
lazy_result = range(1, 31) |> map(fizzbuzz) |> lazyseq()
pvector(lazy_result)
In [64]:
pvector(lazy_result)
In [65]:
pvector(lazy_result)
In [66]:
macro rest_if_first_is_true(first, &args):
match first:
quote(True): quasi_quote(v(unquote_splicing(args)))
_: quote(False)
rest_if_first_is_true(True, 1, 2, 3)
In [67]:
rest_if_first_is_true("foo", 1, 2, 3)
In [ ]:
macro pipeline(&args):
[Symbol('|>')] + args
pipeline([1, 2, 3],
map(-> $1 * 2),
filter(-> $1 != 2),
pvector())
# => pvector([4, 6])
```
### Including a file at compile time
```sh
$ cat anko.mochi
x = 10000
y = 20000
```
```python
require 'anko.mochi'
x
# => 10000
x = 30000
require 'anko.mochi' # include once at compile time
x
# => 30000
```
### Module
```python
module Math:
export add, sub
def add(x, y):
x + y
def sub(x, y):
x - y
Math.add(1, 2)
# => 3
```
```sh
$ cat foobar.mochi
foo = 'foo'
bar = 'bar'
```
```python
require 'foobar.mochi'
[foo, bar]
# => pvector(['foo', 'bar'])
foo = 'foofoofoo'
module X:
export foobar
require 'foobar.mochi'
def foobar:
[foo, bar]
X.foobar()
# => pvector(['foo', 'bar'])
[foo, bar]
# => pvector(['foofoofoo', 'bar'])
```
## TODO
- Documentation
- Improvement of parsing
- Support class definition
## License
MIT License
## Author
[i2y] (https://github.com/i2y)